<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/** 
* Rotator Class 
* 
* Class to rotate based on sort
*
* @category	Libraries
* @subpackage Custom Libraries
* @author Andar Harsono
*/

class Myrotator{

	var $CI;
	var $table_name;
	var $where_field;
	var $where_value;
	
	function Myrotator()
	{
		$this->CI =& get_instance();
	}
	
	function rotate($table,$where_field,$where_value)
	{
		$this->table_name = $table;
		$this->where_field = $where_field;
		$this->where_value = $where_value;
		$this->run();
	}
	
	function run()
	{
		$count = $this->get_datas('id')->num_rows();
		$list = $this->get_datas('id,sort')->result_array();
		
		$n = 0;
		foreach($list as $item)
		{
			$data = array('sort' => $n);
			$this->CI->db->where('id', $item['id']);
			$this->CI->db->update($this->table_name, $data); 
			$n++;
		}
		
		$data = array('sort' => ($count));
		$this->CI->db->where('sort <=',0);
		$this->CI->db->update($this->table_name, $data); 
	}
	
	function get_datas($field)
	{
		$this->CI->db->select($field);
		$this->CI->db->where($this->where_field,$this->where_value);
		$this->CI->db->where('status_id', '1');
		$this->CI->db->orderby('sort','ASC');		
		
		$query = $this->CI->db->get($this->table_name);	
		return $query;
	}	
	
}

/* End of file Mymenu.php */ 
/* Location: ./system/application/libraries/Myrotator.php */ 